﻿app.factory("formControlService", function ($compile) {
    function controlEntity(name, value, type, isnull, length, pointlength/*, isdict*/) {
        this.name = name;
        this.value = value;
        this.type = type;
        this.isnull = isnull;
        this.length = length;
        this.pointlength = pointlength;
        //        this.isdict = isdict;
    };

    function controlHtmlEntity(name, controlDef, controlHtmlEntity) {
        this.name = name;
        this.controlDef = controlDef;
        this.controlHtmlEntity = controlHtmlEntity;
    };

    var formControl = {};
    formControl.getControls = function (path) {
        var controlsEntity = [
            { name: "Name", value: "字典体系名称", type: "varchar", isnull: false, length: "50", pointlength: null },
            { name: "InputCode", value: "输入码", type: "varchar", isnull: false, length: "50", pointlength: null },
            { name: "EnglishName", value: "英文名", type: "varchar", isnull: false, length: "50", pointlength: null },
            { name: "RecommendFlag", value: "是否推荐", type: "varchar", isnull: false, length: "50", pointlength: null },
            { name: "Level", value: "层级", type: "varchar", isnull: false, length: "50", pointlength: null },
            { name: "StatusCode", value: "状态", type: "varchar", isnull: false, length: "50", pointlength: null },
            { name: "StatusCode1", value: "状态1", type: "varchar", isnull: false, length: "50", pointlength: null }
        ];
        var controlEntitys = [];
        for (var i = 0; i < controlsEntity.length; i++) {
            var controlEntityItem = new controlEntity(controlsEntity[i].name, controlsEntity[i].value, controlsEntity[i].type, controlsEntity[i].isnull, controlsEntity[i].length, controlsEntity[i].pointlength);
            controlEntitys.push(controlEntityItem);
        }
        return controlEntitys;
    }

    formControl.getControlHtmlEntitys = function (scope, controlEntitys) {
        var controlHtmlEntitys = [];
        var controlDefs = scope.controlDefs;

        var controlHtml;
        for (var i = 0; i < controlEntitys.length; i++) {
            var controlDef;
            for (var j = 0; j < controlDefs.length; j++) {
                if (controlDefs[j].name == controlEntitys[i].name) {
                    controlDef = controlDefs[j];
                    break;
                }
            }

            var htmlParams = "";
            if (controlDef.required == true) {
                htmlParams += " required";
            }
            if (controlDef.model) {
                htmlParams += " ng-model=\"" + controlDef.model + "\"";
            }
            if (controlDef.placeholder) {
                htmlParams += " placeholder=\"" + controlDef.placeholder + "\"";
            }
            if (controlDef.onclick1) {
                htmlParams += "  ng-click=\"" + controlDef.onclick1 + "\"";
            }
            switch (controlDef.type) {
                case "input":
                    controlHtml = "<input type=\"text\" class=\"form-control\" " + htmlParams + "/>";
                    var controlHtmlEntityItem = new controlHtmlEntity(controlEntitys[i].name, controlDef, controlHtml);
                    controlHtmlEntitys.push(controlHtmlEntityItem)
                    break;
                case "select":
                    controlHtml = "<select class=\"form-control\" " + htmlParams + "></select>";
                    var controlHtmlEntityItem = new controlHtmlEntity(controlEntitys[i].name, controlDef, controlHtml);
                    controlHtmlEntitys.push(controlHtmlEntityItem)
                    break;
                case "checkbox":
                    controlHtml = "<input type=\"checkbox\" " + htmlParams + "/>";
                    var controlHtmlEntityItem = new controlHtmlEntity(controlEntitys[i].name, controlDef, controlHtml);
                    controlHtmlEntitys.push(controlHtmlEntityItem)
                    break;
                case "datetime":
                    break;
                default:
                    break;
            }
        }
        return controlHtmlEntitys;
    }

    formControl.getControlHtmlPanelEntitys = function (scope, controlHtmlEntitys) {
        var controlHtmlPanelEntitys = [];
        var controlDefs = scope.controlDefs;
        var controlHtml;
        var groupID;
        var controlHtmlPanelEntity;
        var index = 0;
        for (var i = 0; i < controlHtmlEntitys.length; i++) {
            var controlDef;
            for (var j = 0; j < controlDefs.length; j++) {
                if (controlDefs[j].name == controlHtmlEntitys[i].name) {
                    controlDef = controlDefs[j];
                    break;
                }
            }
            if (groupID != controlDef.groupid) {
                groupID = controlDef.groupid;
                controlHtmlPanelEntitys[index] = {};
                controlHtmlPanelEntitys[index].groupID = groupID;
                controlHtmlPanelEntitys[index].controls = [];
                controlHtmlPanelEntitys[index].controls.push(controlHtmlEntitys[i]);
                controlHtmlPanelEntity = controlHtmlPanelEntitys[index];
            } else {
                controlHtmlPanelEntity.controls.push(controlHtmlEntitys[i]);
            }
            index++;
        }
        return controlHtmlPanelEntitys;
    }

    formControl.generateControls = function (scope, element) {
        //控件实体
        var controlEntitys = formControl.getControls(scope.path);
        scope.controlEntitys = controlEntitys;
        //控件HTML实体
        var controlHtmlEntitys = formControl.getControlHtmlEntitys(scope, controlEntitys);
        scope.controlHtmlEntitys = controlHtmlEntitys;

        var controlHtmlPanelEntitys = formControl.getControlHtmlPanelEntitys(scope, controlHtmlEntitys);
        scope.controlHtmlPanelEntitys = controlHtmlPanelEntitys;
        var panelHtml = "";
        for (var i = 0; i < controlHtmlPanelEntitys.length; i++) {
            panelHtml += "<div class=\"form-group\">";
            panelHtml += formControl.generateControl(scope, controlHtmlPanelEntitys[i]);
            panelHtml += "</div>";
        }
        //按钮部分
        panelHtml += "<div class=\"line line-dashed b-b line-lg\"></div>" +
                        "<div class=\"form-group\">" +
                            "<div class=\"col-lg-10 col-lg-offset-2\">" +
                                "<button type=\"submit\" class=\"btn btn-success\" ng-click=\"ok()\">保存</button>" +
                                "<button type=\"submit\" class=\"btn btn-primary\" ng-click=\"okNext()\">保存并继续添加</button>" +
                                "<button type=\"submit\" class=\"btn btn-default\" ng-click=\"cancel()\">取消</button>" +
                            "</div>" +
                        "</div>"
        var linkFn = $compile(panelHtml);
        element.append(linkFn(scope));
    }

    formControl.generateControl = function (scope, controlHtmlPanelEntity) {
        var panelHtml = "";
        var controlDefs = scope.controlDefs;
        for (var i = 0; i < controlHtmlPanelEntity.controls.length; i++) {
            var controlDef;
            for (var j = 0; j < scope.controlDefs.length; j++) {
                if (controlDefs[j].name == controlHtmlPanelEntity.controls[i].name) {
                    controlDef = controlDefs[j];
                    break;
                }
            }
            if (i == 0) {
                panelHtml += formControl.generateControlContent(scope, controlDef, controlHtmlPanelEntity.controls[i], controlHtmlPanelEntity.controls.length,true);
            } else {
                panelHtml += formControl.generateControlContent(scope, controlDef, controlHtmlPanelEntity.controls[i], controlHtmlPanelEntity.controls.length,false);
            }
            
        }
        return panelHtml;
    }

    formControl.generateControlContent = function (scope, controlDef, controlHtmlEntity, controlHtmlEntitysLength,isFirstColunm) {
        //var panelHtml = "";
        //var leftlength = 2;
        //var rightlength = 10;
        //var length = controlHtmlEntitysLength;
        //var partLength = rightlength / length;
        //var labelLength;
        //var divLength;
        //if (isFirstColunm==false) {
        //    labelLength = ((partLength * 0.2) > parseInt((partLength * 0.2).toString().split(".")[0]))
        //                        ? parseInt((partLength * 0.2).toString().split(".")[0]) + 1
        //                        : parseInt((partLength * 0.2).toString().split(".")[0]);
        //    divLength = ((partLength * 0.8) > parseInt((partLength * 0.8).toString().split(".")[0]))
        //                        ? parseInt((partLength * 0.8).toString().split(".")[0]) + 1
        //                        : parseInt((partLength * 0.8).toString().split(".")[0]);
        //} else {
        //    divLength = ((partLength) > parseInt((partLength).toString().split(".")[0]))
        //                        ? parseInt((partLength).toString().split(".")[0]) + 1
        //                        : parseInt((partLength).toString().split(".")[0]);
        //}
        
        //if (labelLength + divLength > partLength) {
        //    if (partLength == rightlength) {
        //        labelLength = labelLength - 1;
        //    } else {
        //        divLength = divLength - 1;
        //    }
        //}
        //if (isFirstColunm) {
        //    labelLength = leftlength;
        //}
        var panelHtml = "";
        var length = controlHtmlEntitysLength;
        var partLength = 12 / length;
        var labelLength = ((partLength * 0.2) > parseInt((partLength * 0.2).toString().split(".")[0]))
                                ? parseInt((partLength * 0.2).toString().split(".")[0]) + 1
                                : parseInt((partLength * 0.2).toString().split(".")[0]);
        var divLength = ((partLength * 0.8) > parseInt((partLength * 0.8).toString().split(".")[0]))
                                ? parseInt((partLength * 0.8).toString().split(".")[0]) + 1
                                : parseInt((partLength * 0.8).toString().split(".")[0]);
        if (labelLength + divLength > partLength) {
            if (partLength == 12) {
                labelLength = labelLength - 1;
            } else {
                divLength = divLength - 1;
            }
        }
        
        switch (controlDef.type) {
            case "input":
                panelHtml = "<label class=\"col-lg-" + labelLength + " control-label\">" + controlDef.showname + "</label>" +
                            "<div class=\"col-lg-" + divLength + "\">" + controlHtmlEntity.controlHtmlEntity + "</div>";
                break;
            case "select":
                panelHtml = "<label class=\"col-lg-" + labelLength + " control-label\">状态</label>" +
                            "<div class=\"col-lg-" + divLength + "\">" + controlHtmlEntity.controlHtmlEntity + "</div>";
                break;
            case "checkbox":
                panelHtml = "<label class=\"col-lg-" + labelLength + " control-label\">" + controlDef.showname + "</label>" +
                            "<div class=\"col-lg-" + divLength + "\">" +
                                "<div class=\"checkbox\">" +
                                    "<label class=\"i-checks\">" +
                                        controlHtmlEntity.controlHtmlEntity + "<i></i>" +
                                    "</label>" +
                                "</div>" +
                            "</div>";
                break;
            case "datetime":
                break;
            default:
                break;
        }
        return panelHtml;
    }

    return formControl;
})

app.directive("autoFormPanel", function ($compile) {
    return {
        restrict: 'EA',
        replace: true,
        link: function (scope, element, attrs) {
            var panelHtml = "<div class=\"panel panel-default\" ng-controller=\"" + scope.tablename + "\">" +
                                "<div class=\"panel-heading font-bold\">字典体系</div>" +
                                "<div class=\"panel-body\">" +
                                "<auto-form></auto-form>" +
                                "</div>" +
                          "</div>"
            var linkFn = $compile(panelHtml);
            element.append(linkFn(scope));
        }
    }
});

app.controller("autoFormController", function ($scope, $attrs, formControlService) {
    this.init = function (element) {
        formControlService.generateControls($scope, element);
    }
});

app.directive("autoForm", function () {
    return {
        restrict: 'EA',
        transclude: true,
        replace: true,
        template: "<form name=\"form\" class=\"bs-example form-horizontal form-validation\" ng-transclude></form>",
        controller: "autoFormController",
        link: function (scope, element, attr, autoFormController) {
            autoFormController.init(element);
        }

    }
});




app.controller("tszdtable", function ($scope) {
    $scope.controlDefs = [
        { name: "Name", showname: "名称", requirefont: "民族的名称", type: "input", groupid: 1, required: true, onclick1: "onclick1()" },
        { name: "InputCode", showname: "输入码", requirefont: "输入码", type: "input", groupid: 2 },
        { name: "EnglishName", showname: "英文名称", requirefont: "民族的英文名称", type: "input", groupid: 3 },
        { name: "RecommendFlag", showname: "是否推荐", requirefont: null, type: "checkbox", groupid: 4 },
        { name: "Level", showname: "层级", requirefont: null, type: "input", groupid: 4 },
        { name: "StatusCode", showname: "状态", requirefont: null, type: "select", groupid: 4 },
        { name: "StatusCode1", showname: "状态1", requirefont: null, type: "select", groupid: 4 },
    ]

    $scope.onclick1 = function () {
        angular.alert("民族");
    }
})